Repository navigation
Conversation
|
Hi there, @gr8man! 👋 It looks like this pull request does not follow our template:
Please update the description to follow the template. The |
Caches parsed cast rules (handler class, extracted parameters) on demand per unique type in DataCaster. This eliminates repeated regex pattern matching (preg_match), string manipulation (explode, trim), and reflection interface checks (is_subclass_of) for every field on every row hydrated during Entity and Model operations. Benchmark on 900,000 castAs() calls: - Before: ~6,440 ms (7.16 us/call) - After: ~2,943 ms (3.27 us/call) - 2.18x speedup (-54% time, >305k casts/s)
4ab52ef to
7777bf0
Compare
neznaika0
left a comment
There was a problem hiding this comment.
It looks good. Will there be any issues for FrankenPHP when there are changes to the Entity in the same loop? Will the cache be updated?
When I work with the entities and custom handlers, we might return to this PR.
|
Thanks @neznaika0 for the review! There are no issues for FrankenPHP:
Sounds good! Happy to revisit when you work on entities and custom handlers. |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are identified for the casting-rule caching changes, though tests were not run and runtime validation remains with CI.
No open issues 🎉
What changed in this PR
Optimizes DataCaster::castAs() by caching compiled casting rules per type, reducing repeated parsing and handler validation during model and entity hydration.
Changes:
- Compiles casting rules on demand and reuses them within each caster instance.
- Preserves cached rules when
setTypes()receives identical mappings; clears them when mappings change. - Adds tests for cache reuse, invalidation, nullable values, and exception behavior.
Assumed target: develop, since no base branch is specified. Tests were not run; runtime validation remains with CI.
| File | Description |
|---|---|
| tests/system/DataCaster/DataCasterTest.php | Adds cache and casting-behavior regression coverage. |
| system/DataCaster/DataCaster.php | Introduces per-type rule caching and conditional invalidation. |
Review effort: Balanced
Description
This PR optimizes
DataCaster::castAs()by compiling and caching type cast rules on demand per unique type inDataCaster::$compiledTypes.Motivation and Context
In
DataCaster::castAs(), hydrating model rows and entity attributes repeatedly triggered the following operations for every single field of every hydrated row:preg_match('/\A(.+)\[(.+)\]\z/', $type, $matches)to detect array/parameterized cast definitions (e.g.,json[array],datetime[Y-m-d]).explode(',', ...)andarray_map(trim(...), ...)to parse parameters.is_subclass_of($handler, CastInterface::class)andis_subclass_of($handler, EntityCastInterface::class)when$strict === false(the default mode used by Entity casting).For a query fetching 1,000 rows with 10 fields per row, this resulted in 10,000 regex executions and 20,000 reflection checks.
By caching the compiled
[$handler, $params]keyed by$type:int,bool,csv) share the compiled rule across the lifetime of the caster.preg_match,explode,trim, andis_subclass_ofexecute only once per unique cast type.$type = $this->types[$field] ?? nullprovides a single array lookup fast path.setTypes(array $types)preserves the compiled cache when called with identical types (asEntity::castAs()does on every field), while safely invalidating$compiledTypesif types are modified dynamically.Benchmark & Performance Impact
Microbenchmark executing 900,000
castAs()calls with a mix of types (int,bool,float,csv,json[array],?int):Backward Compatibility (100% BC)
DataCasteris afinalclass with private properties; no public or protected API signatures have changed.?type) immediately returnsnull.InvalidArgumentException('Field "..." is not nullable, but null was passed.')before checking handler validity.CastException::forInvalidMethod()upfront.Checklist